package com.astrocsr.dao;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astrocsr.common.db.SearchQueryTemplate;
import com.astrocsr.domain.bean.TblAccount;
import com.astrocsr.domain.bean.TblGroupAccount;
import com.astrocsr.domain.condition.AccountCondition;
import com.astrocsr.domain.db.PagingInfo;
import com.astrocsr.domain.db.SearchResult;

@Repository("groupAccountDAO")
public class GroupAccountDAO extends BaseHelperDAO {

	public SearchResult<TblGroupAccount> listByGroup(AccountCondition condition, PagingInfo pagingInfo) throws Exception {
		
		// Prepare
		SearchResult<TblGroupAccount> result = null;
		String orderColumn = "accountNo";
		SearchQueryTemplate searchQueryTemplate = new SearchQueryTemplate(orderColumn, pagingInfo);
		searchQueryTemplate.setQueryString("from TblGroupAccount model");

		if (condition != null) {
			if (StringUtils.isNotBlank(condition.getGroupCode())) {
				searchQueryTemplate.setExpression("model.groupCode = :groupCode", "groupCode", condition.getGroupCode());
			}
			if (StringUtils.isNotBlank(condition.getAccountNo())) {
				String accountNo = "%" + condition.getAccountNo() + "%";
				searchQueryTemplate.setExpression("model.accountNo like :accountNo", "accountNo", accountNo);
			}
		}

		// execute search with paging
		result = this.find(searchQueryTemplate);
		
		return result;
	}

	public SearchResult<TblGroupAccount> listByAccount(String accountNo) throws Exception {

		// Prepare
		SearchResult<TblGroupAccount> result = null;
		String orderColumn = "accountNo";
		SearchQueryTemplate searchQueryTemplate = new SearchQueryTemplate(orderColumn, null);
		searchQueryTemplate.setQueryString("from TblGroupAccount where accountNo = :accountNo");

		// Set parameters
		searchQueryTemplate.setParameter("accountNo", accountNo);

		// execute search with paging
		result = this.find(searchQueryTemplate);
		
		return result;
	}

	public SearchResult<TblAccount> listAllAccount(AccountCondition condition, PagingInfo pagingInfo) throws Exception {

		// Prepare
		SearchResult<TblAccount> result = null;
		String orderColumn = "accountNo";
		SearchQueryTemplate searchQueryTemplate = new SearchQueryTemplate(orderColumn, pagingInfo);
		searchQueryTemplate.setQueryString("from TblAccount model ");
		
		if (condition != null) {
			if (StringUtils.isNotBlank(condition.getAccountNo())) {
				String accountNo = "%" + condition.getAccountNo() + "%";
				searchQueryTemplate.setExpression("model.accountNo like :accountNo", "accountNo", accountNo);
			}
		}

		// execute search with paging
		result = this.find(searchQueryTemplate);
		
		return result;
	}

	/**
	 * Insert customer into database
	 */
	public void addGroupAccount(TblGroupAccount entity) throws Exception {
		// Insert into database
		getCurrentSession().save(entity);
	}
	
	public boolean checkAssignedAccount(String selectedAccountNo, String selectedGroupNo) throws Exception {
		try {
			String queryString = "from TblGroupAccount where accountNo = :accountNo and groupCode = :groupCode";
			Query query = getCurrentSession().createQuery(queryString);
			query.setParameter("accountNo", selectedAccountNo);
			query.setParameter("groupCode", selectedGroupNo);
			query.setMaxResults(1);
			return (query.uniqueResult() == null)? false: true;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public void deleteGroupAccount(Long groupAccountId) {
		Query query = getCurrentSession().createQuery("DELETE from TblGroupAccount WHERE groupAccountId =:groupAccountId ");
		query.setParameter("groupAccountId", groupAccountId);
		query.executeUpdate();
	}
}